#include "UMLDiagram.h"

UMLDiagram::UMLDiagram()
{
	// default is fine
}

UMLDiagram::~UMLDiagram()
{
	// Delete all known UMLObjects since we own them.
	for (known_type::iterator iter = _known.begin(); iter != _known.end();
			++iter)
		delete *iter;
}

QSize UMLDiagram::size() const
{
	return _size;
}

UMLObject& UMLDiagram::add(UMLObject& theirs)
{
	UMLObject *ours;
	known_type::iterator k = _known.find(&theirs);

	// check if we already own the passed object
	if (k == _known.end())
	{
		// gotta copy
		ours = theirs.clone();
		_known.insert(ours);
	}
	else
	{
		ours = *k;
	}

	_active.push_back(ours);
	ours->attach(this);

	// TODO: neighbor checking?
	
	notifyObservers();
	
	return *ours;
}

void UMLDiagram::remove(iterator position)
{
	UMLObject* object = *position; // save before we invalidate

	object->unattach(this);
	_active.erase(position);

	// TODO: neighbor checking?
	
	emit objectRemoved(object);
	notifyObservers();
}

void UMLDiagram::update(Observable*)
{
	// just forward the notification
	notifyObservers();
}

